package com.light.gamecomponents.physics;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.CullingSprite;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class MainPieces extends PhysicsComponents implements ContactListener{
	
	//private Sound m_collideSound;
	
	public static final int INITX = -5000;
	public static final int INITY = -5000;
	
	public MainPieces(Roller game, String resourceName, Vector2 initialPosition, Vector2 size) 
	{
		super(game, resourceName, initialPosition, size);
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {

	}
	
	public void setBodyPosition(Vector2 position)
	{
		this.m_body.setTransform(position, 0);
	}
	
	public void setVelocity(Vector2 velocity)
	{
		m_body.setLinearVelocity(velocity);
	}

	public boolean isOutside()
	{
		if(this.m_sprite.getX()<INITX+1000&&this.m_sprite.getY()<INITY+1000)
		{
			return true;
		}
		else return false;
	}
	
	@Override
	public void reset() {
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
		//ResourceManager.onLoadSoundResources("Box_Crack", m_game);
		//m_collideSound = ResourceManager.getSound("Box_Crack");
	}
	
	@Override
	public void unLoadResources() {
		m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(50, 0.0f, 1.0f);
		
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_NORMAL;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_NORMAL;
		
		m_sprite = new CullingSprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
                              m_textureRegion);
		
		CullingSprite temp_sprite = new CullingSprite(m_initialPosition.x, 
	  		      m_initialPosition.y,
                  m_textureRegion);
		temp_sprite.setScale(0.75f);
		
		//m_sprite.setSize(this.m_size.x, this.m_size.y);
		
		m_body = PhysicsFactory.createBoxBody(physicsWorld, 
												temp_sprite, 
											  BodyType.DynamicBody, 
											  objectFixtureDef);
		
		
		
		scene.attachChild(m_sprite);
		m_physicsConnector = new PhysicsConnector(m_sprite, m_body, true, true);
		physicsWorld.registerPhysicsConnector(m_physicsConnector);
		((RollerPhysicsWorld)physicsWorld).registerContactListener(this);
		
		
	}
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		physicsWorld.unregisterPhysicsConnector(m_physicsConnector);
		scene.detachChild(m_sprite);
		physicsWorld.destroyBody(m_body);
	
	}

	@Override
	public void onLoadEngine() {
		
	}

	@Override
	public void setPosition(int x, int y) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beginContact(Contact contact) {
		if (contact.getFixtureA().getBody() == this.m_body || 
			contact.getFixtureB().getBody() == this.m_body)
		{
			Vector2 vecNormal = contact.GetWorldManifold().getNormal();
		    Vector2 vecImpulse = new Vector2(vecNormal.x * m_body.getLinearVelocity().x, 
		    		                         vecNormal.y * m_body.getLinearVelocity().y); 
			
			float v = vecImpulse.x * vecImpulse.x + 
					  vecImpulse.y * vecImpulse.y;
			
			if (v >= 25)
			{
				//m_collideSound.play();
			}
		}
	}

	@Override
	public void endContact(Contact contact) {
		// TODO Auto-generated method stub
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void stopBody()
	{
		if(this.isOutside())m_body.setType(BodyType.StaticBody);
		else m_body.setType(BodyType.DynamicBody);
	}
	@Override
	public void startBody()
	{
		m_body.setType(BodyType.DynamicBody);
	}
}